package com.myrent.common;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;


@Data
public class ReturnData {
    private Integer code;
    private String message;
    private Object data;

    public ReturnData(Integer code, String message, Object data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ReturnData(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public static ReturnData success(Object data) {
        return new ReturnData(200, "请求成功", data);
    }

    public static ReturnData success(String msg) {
        return new ReturnData(200, msg, null);
    }

    public static ReturnData success(String message, Object data) {
        return new ReturnData(200, message, data);
    }

    public static ReturnData success() {
        return new ReturnData(200, "请求成功");
    }

    public static ReturnData fail(Integer code, String message) {
        return new ReturnData(code, message);
    }

    @Override
    public String toString() {
        ObjectMapper mapper=new ObjectMapper();
        try {
            return mapper.writeValueAsString(this);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return "{}";
    }
}
